package com.fruit.servlets;
import com.fruit.dao.FruitDao;
import com.fruit.dao.impl.FruitDaoImpl;
import com.fruit.pojo.Fruit;
import com.myssm.myspringmvc.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//@WebServlet("/add.do")
public class AddServlet extends ViewBaseServlet {
    private FruitDao fruitDao = new FruitDaoImpl();

    //    doPost
    @Override
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException, IOException {
        //1、设置字符编码集编码
        request.setCharacterEncoding("UTF-8");
        List<Object> objects = new ArrayList<>();
        //2、获取水果名字
        String fname = request.getParameter("fname");
        //2、获取水果价格
        String priceStr = request.getParameter("price");
        //3、把价格从String转为Integer
        Integer price = Integer.parseInt(priceStr);
        String fcountStr = request.getParameter("fcount");
        Integer fcount = Integer.parseInt(fcountStr);
        String remark = request.getParameter("remark");
        Fruit fruit = new Fruit(0, fname, price,
                fcount, remark);
        //5、调用添加水果方法，添加水果数据
        fruitDao.addFruit(fruit);
        //6、输出添加水果执行结果
//        /index.html
        response.sendRedirect("index");

    }
}
